package com.logic.idevice.impl.common

import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import com.logic.idevice.IUHFScan
import com.myutils.UHFHelp
import com.myutils.UHFUtils
import com.reader.helper.OperateTagBuffer
import com.sinopec.basemodule.BaseApplication

/**
 * create by lwj at 2022/7/20
 * 通用型设备的扫描功能
 */
class CommonScan(
    override val callRefreshText: IUHFScan.IRefreshSpeed,
    override val callDataRefresh: IUHFScan.IRefreshData,
    override val callScanStatusChange: IUHFScan.IScanStatus
) : IUHFScan {

    private val mItemList = ArrayList<String>()

    //    private String mDeviceId = "";
    private var mUhfHelp: UHFHelp? = null

    override fun init(activity: AppCompatActivity) {
        activity.lifecycle.addObserver(LifecycleEventObserver { source, event ->
            when (event) {
                Lifecycle.Event.ON_CREATE -> {
                    initUhf(activity)
                }
                Lifecycle.Event.ON_START -> {
                    if (mUhfHelp == null) {
                        mUhfHelp = UHFHelp.newInstance()
                    }
                }
                Lifecycle.Event.ON_RESUME -> {
                    if (mUhfHelp != null) {
                        mUhfHelp!!.onResume()
                    }
                }
                Lifecycle.Event.ON_PAUSE -> {
                    if (mUhfHelp != null) {
                        mUhfHelp!!.onPause()
                    }
                }
                Lifecycle.Event.ON_DESTROY -> {
                    if (mUhfHelp != null) {
                        mUhfHelp!!.onDestroy(activity)
                    }
                }
            }
        })
    }

    private fun callRefreshSpeed() {

        var time =
            (UHFHelp.getM_curOperateTagBuffer().dtEndTag.time - UHFHelp.getM_curOperateTagBuffer().dtStartTag.time) / 1000
        if (time < 0) {
            time = 0
        }
        callRefreshText.onRefreshSpeed(
            mItemList.size,
            UHFHelp.getM_curOperateTagBuffer().nReadRate,
            time
        )
    }

    private fun callRefreshData() {
        this@CommonScan.refreshList()
        callDataRefresh.onRefreshData(mItemList)
    }

    override fun startOrStop(isStart: Boolean) {
        mUhfHelp?.startStopBt(isStart)
    }

    override fun isCloseRfid(): Boolean {
        return true
    }

    override fun isScanning(): Boolean {
        return mUhfHelp!!.isKeyPressing
    }

    override fun clear() {
        mUhfHelp?.clearList()
    }

    override fun newestData(): List<String> {
        return UHFHelp.getM_curOperateTagBuffer().lsTagList.map { it.strData }
    }

    private fun initUhf(context: AppCompatActivity) {
        UHFHelp.newInstance().initConnectUtils(BaseApplication.getInstance());
        if (mUhfHelp == null) {
            mUhfHelp = UHFHelp.newInstance()
        }
        mUhfHelp!!.initUHF()
        mUhfHelp!!.addScanCodeReceiver(context)
        mUhfHelp!!.setScanCallback(object : UHFUtils.ScanCallback {
            override fun refreshText() {
                if (context.isFinishing) {
                    return
                }
                callRefreshSpeed()
            }

            override fun refreshList() {
                if (context.isFinishing) {
                    return
                }
                callRefreshData()
            }

            override fun refreshStartStop(start: Boolean) {
                if (context.isFinishing) {
                    return
                }
                callScanStatusChange.onScanStatusChange(start)
            }
        })
    }

    private fun refreshList() {
        val list = UHFHelp.getM_curOperateTagBuffer().lsTagList
//        val dtIndexMap = UHFHelp.getM_curOperateTagBuffer().dtIndexMap
        mItemList.clear()
        if (list != null && !list.isEmpty()) {
            mItemList.addAll(ArrayList(list).map { operateTagMap: OperateTagBuffer.OperateTagMap -> operateTagMap.strData })
        }
    }

}